package com.servlet;

import java.io.IOException; 
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;

import com.iflytek.MscTest;
import com.util.Audiotranscoding;
import com.util.DownLoadFromUrl;

/**
 * Servlet implementation class SpeechrecognitionServlrt
 */
@WebServlet("/SpeechrecognitionServlrt")
public class SpeechrecognitionServlrt extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public SpeechrecognitionServlrt() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws IOException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		System.out.println(request.getContextPath());
		/*System.out.println(request.getSession().getServletContext().getRealPath(""));*/

		
		
		/*//第一步： 下载amr文件
		String url=request.getParameter("url");
		DownLoadFromUrl.downLoadFromUrl(url, "amrfile", "E:/");
		//第二步：转换格式
		Audiotranscoding.changeToPcm("E:\\amrfile", "E:\\test.pcm");*/
		InputStream is = request.getInputStream();  
		System.out.println(request.getHeader("Accept"));
        
		//第一步： 下载amr文件
		Map map=request.getParameterMap();
		String url=(String) map.get("url");
		DownLoadFromUrl.downLoadFromUrl(url, "amrfile", request.getContextPath());
		//第二步：转换格式
		Audiotranscoding.changeToPcm(request.getContextPath()+"\\amrfile", request.getContextPath()+"\\test.pcm");
		//第三步：语音转换
		MscTest msctest=new MscTest();
		try {
			msctest.speechrecongnition(response,request.getContextPath()+"\\test.pcm");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
	public static final byte[] readBytes(InputStream is, int contentLen)  
    {  
        if (contentLen > 0)  
        {  
            int readLen = 0;  
            int readLengthThisTime = 0;  
            byte[] message = new byte[contentLen];  
            try  
            {  
                while (readLen != contentLen)  
                {  
                    readLengthThisTime = is.read(message, readLen, contentLen - readLen);  
                    if (readLengthThisTime == -1)  
                    {// Should not happen.  
                        break;  
                    }  
                    readLen += readLengthThisTime;  
                }  
                return message;  
            }  
            catch (IOException e)  
            {  
                // Ignore  
                e.printStackTrace();  
            }  
        }  
  
        return new byte[] {};  
    }  


}
